//
//  MZYupdateViewController.m
//  WristWatch
//
//  Created by dllo on 15/10/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MZYupdateViewController.h"
#import "MZYupdateCollectionViewCell.h"
#import "MZYHeader.h"
#import "MZYupdateWebViewController.h"

@interface MZYupdateViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)UICollectionView *updateCollectionView;
@property(nonatomic, retain)NSMutableArray *updateArr;
@property(nonatomic, retain)MBProgressHUD *HUD;
@property(nonatomic, assign)NSInteger page;

@end

@implementation MZYupdateViewController

- (void)dealloc {
    [_updateCollectionView release];
    [_updateArr release];
    [_HUD release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.updateArr = [NSMutableArray array];
    [self createView];
    [self createData];
}

- (void)createView {
    UICollectionViewFlowLayout *upflowLayout = [[UICollectionViewFlowLayout alloc] init];
    upflowLayout.itemSize = CGSizeMake((WIDTH - 60) / 3, HEIGHT / 5.5);
    upflowLayout.minimumInteritemSpacing = 10;
    upflowLayout.minimumLineSpacing = 10;
    upflowLayout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
    
    self.updateCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 10, WIDTH, HEIGHT) collectionViewLayout:upflowLayout];
    [self.view addSubview:self.updateCollectionView];
    [_updateCollectionView release];
    self.updateCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.updateCollectionView.dataSource = self;
    self.updateCollectionView.delegate = self;
    
    [self.updateCollectionView registerClass:[MZYupdateCollectionViewCell class] forCellWithReuseIdentifier:@"reusera"];
    
    //    //下拉
    //    self.updateCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(heade)];
    //    //上啦
    //    self.updateCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(foote)];
    
    self.page = 1;
    
}

- (void)createData {
    // 解析updateCollectionView
    AFHTTPRequestOperationManager *upmanager = [AFHTTPRequestOperationManager manager];
    [upmanager GET:@"http://www.xbiao.com/topic/newslist/type/9/catid/34/year/2015/version/2_0" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSMutableArray *cArr = dic[@"data"];

        self.updateArr = [MZYInformationModel MZYBaseModelByArr:cArr];
        
        if (self.updateArr.count > 0) {
            self.HUD.hidden = YES;
            [self.updateCollectionView reloadData];
        }
        [self.updateCollectionView.header endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}

#pragma mark CollectionView的协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.updateArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MZYupdateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reusera" forIndexPath:indexPath];
    MZYInformationModel *info = self.updateArr[indexPath.row];
    cell.upLabel.text = info.title;
    [cell.upImageView sd_setImageWithURL:[NSURL URLWithString:info.thumb] placeholderImage:[UIImage imageNamed:@"3.jpg"]];
    cell.layer.cornerRadius = 10;
    cell.layer.masksToBounds = YES;
    return cell;
 
}

#pragma mark collectionView 跳转web下一界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"我被点了💢");
    MZYupdateWebViewController *upVC = [[MZYupdateWebViewController alloc] init];
    MZYInformationModel *info = self.updateArr[indexPath.row];
    upVC.url = info.url;
    upVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:upVC animated:YES];
    [upVC release];

}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
